import sqlite3
import os

Table1 = """
create table bonus
(
ename varchar(10),
job varchar(9),
sal decimal(7,2),
comm decimal(7,2)
);
"""
Table2 = """
create table dept
(
deptno integer(4) primary key not null,
dname varchar(14),
loc varchar(13)
);
"""
Table3 = """
create table salgrade
(
grade integer(1),
losal decimal(7,2),
hisal decimal(7,2)
);
"""
SQL = """
create table emp
(
empno integer(4) primary key not null,
ename varchar(10),
job varchar(9),
mgr integer(4),
hiredate date,
sal decimal(7,2),
comm decimal(7,2),
deptno integer(2),
CONSTRAINT fk_deptno
FOREIGN KEY (deptno)
REFERENCES dept(deptno)
);
"""
SQL2 = """
insert into DEPT (DEPTNO, DNAME, LOC) values (10, 'ACCOUNTING', 'NEW YORK');
insert into DEPT (DEPTNO, DNAME, LOC) values (20, 'RESEARCH', 'DALLAS');
insert into DEPT (DEPTNO, DNAME, LOC) values (30, 'SALES', 'CHICAGO');
insert into DEPT (DEPTNO, DNAME, LOC) values (40, 'OPERATIONS', 'BOSTON');

insert into SALGRADE (GRADE, LOSAL, HISAL) values (1, 700, 1200);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (2, 1201, 1400);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (3, 1401, 2000);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (4, 2001, 3000);
insert into SALGRADE (GRADE, LOSAL, HISAL) values (5, 3001, 9999);

insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7369, 'SMITH', 'CLERK', 7902, DATE('1980-12-17'),  800.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7499, 'ALLEN', 'SALESMAN', 7698, DATE('1981-02-20'),  1600.00, 300.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7521, 'WARD', 'SALESMAN', 7698, DATE('1981-02-22'),  1250.00, 500.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7566, 'JONES', 'MANAGER', 7839, DATE('1981-04-02'),  2975.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7654, 'MARTIN', 'SALESMAN', 7698, DATE('1982-03-08'),  1250.00, 1400.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7698, 'BLAKE', 'MANAGER', 7839, DATE('1987-08-28'),  2850.00, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7782, 'CLARK', 'MANAGER', 7839, DATE('1981-05-12'), 2450.00, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7788, 'SCOTT', 'ANALYST', 7566, DATE('1982-03-01'),  3000.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7839, 'KING', 'PRESIDENT', null, DATE('1981-06-17'), 5000.00, null, 10);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7844, 'TURNER', 'SALESMAN', 7698, DATE('1981-09-08'),  1500.00, 0.00, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7876, 'ADAMS', 'CLERK', 7788, DATE('1981-05-23'),  1100.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7900, 'JAMES', 'CLERK', 7698, DATE('1981-12-03'), 950.00, null, 30);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7902, 'FORD', 'ANALYST', 7566, DATE('1981-12-03'),  3000.00, null, 20);
insert into EMP (EMPNO, ENAME, JOB, MGR, HIREDATE,SAL, COMM, DEPTNO) values (7934, 'MILLER', 'CLERK', 7782, DATE('1982-01-23'), 1300.00, null, 10);
"""

os.remove("demo1.db")

con = sqlite3.connect("demo1.db")
cs = con.cursor()

cs.execute(Table1)
con.commit()
cs.execute(Table2)
con.commit()
cs.execute(Table3)
con.commit()
cs.execute(SQL)
con.commit()
for x in SQL2.split('\n'):
  cs.execute(x)
  con.commit()
cs.execute("select * from emp")

rows = cs.fetchall()

for row in rows:
  print(row[0],row[1],row[2],row[3],row[4])

con.commit()
cs.close()
con.close()